package com.guodu.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.UnitsDao;
import com.guodu.dao.ViewMaterialZzlDao;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialZzl;
import com.guodu.service.ViewMaterialZzlService;
import com.guodu.vo.ViewMaterialZzlVo;
import com.guodu.vo.ViewVo;

@Service("viewMaterialZzlService")
public class ViewMaterialZzlServiceImpl implements ViewMaterialZzlService{

	@Resource
	private ViewMaterialZzlDao viewMaterialZzlDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public Map<String, ViewMaterialZzlVo> findByVo(ViewVo vvo) {
		// TODO Auto-generated method stub
		List<Units> funitses = unitsDao.findAllFunits();
		List<ViewMaterialZzl> views = viewMaterialZzlDao.findByVo(vvo);
		List<ViewMaterialZzl> views2 = viewMaterialZzlDao.findByVo2(vvo);
		Map<String, ViewMaterialZzlVo> map = new HashMap<String, ViewMaterialZzlVo>();
		DecimalFormat df = new DecimalFormat("##.00%");
		map.put("all", new ViewMaterialZzlVo(new ViewMaterialZzl()));
		for(Units units : funitses){
			map.put(units.getUnitsId().toString()+"all",new ViewMaterialZzlVo(new ViewMaterialZzl()));
		}
		for(int i =1;i<=views.size();i++){
			views.get(i-1).setKczzlId(Long.valueOf(i));
		}
		for(int i =1;i<=views2.size();i++){
			for(ViewMaterialZzl view : views){
				if(view.getUnitsId()==views2.get(i-1).getUnitsId()){
					view.setRkzzlId(Long.valueOf(i));
				}
			}
		}
		for(ViewMaterialZzl view:views){
			this.sum(map.get("all"), view);
			this.sum(map.get(view.getUnitsId()==null?"group":view.getFunitsId().toString()+"all"), view);
			ViewMaterialZzlVo viewMaterialZzlVo = new ViewMaterialZzlVo(view);
			if(view.getKczzl()!=null){
			viewMaterialZzlVo.setKczzl(df.format(view.getKczzl()));
			}else{
				viewMaterialZzlVo.setKczzlId(null);
			}
			if(view.getRkzzl()!=null){
			viewMaterialZzlVo.setRkzzl(df.format(view.getRkzzl()));
			}else{
				viewMaterialZzlVo.setRkzzlId(null);
			}
			map.put(view.getUnitsId().toString(), viewMaterialZzlVo);
		}
		if(map.get("all").getKcMoney()!=null&&map.get("all").getCkMoney()!=null&&map.get("all").getKcMoney().intValue()>0){
			map.get("all").setKczzl(df.format(map.get("all").getCkMoney().divide(map.get("all").getKcMoney(),4,BigDecimal.ROUND_HALF_EVEN)));
		}
		if(map.get("all").getRkMoney()!=null&&map.get("all").getRkMoney()!=null&&map.get("all").getKcMoney().intValue()>0){
			map.get("all").setRkzzl(df.format(map.get("all").getRkMoney().divide(map.get("all").getKcMoney(),4,BigDecimal.ROUND_HALF_EVEN)));
		}
		List<ViewMaterialZzlVo> rkviewMaterialZzlVos = new ArrayList<ViewMaterialZzlVo>();
		List<ViewMaterialZzlVo> kcviewMaterialZzlVos = new ArrayList<ViewMaterialZzlVo>();
		for(int i =0 ; i<funitses.size();i++){
			
			if(map.get(funitses.get(i).getUnitsId()+"all").getKcMoney()!=null&&map.get(funitses.get(i).getUnitsId()+"all").getCkMoney()!=null&&map.get(funitses.get(i).getUnitsId()+"all").getKcMoney().intValue()>0){
				map.get(funitses.get(i).getUnitsId()+"all").setKczzl(df.format(map.get(funitses.get(i).getUnitsId()+"all").getCkMoney().divide(map.get(funitses.get(i).getUnitsId()+"all").getKcMoney(),4,BigDecimal.ROUND_HALF_EVEN)));
			}
			if(map.get(funitses.get(i).getUnitsId()+"all").getRkMoney()!=null&&map.get(funitses.get(i).getUnitsId()+"all").getRkMoney()!=null&&map.get(funitses.get(i).getUnitsId()+"all").getKcMoney().intValue()>0){
				map.get(funitses.get(i).getUnitsId()+"all").setRkzzl(df.format(map.get(funitses.get(i).getUnitsId()+"all").getRkMoney().divide(map.get(funitses.get(i).getUnitsId()+"all").getKcMoney(),4,BigDecimal.ROUND_HALF_EVEN)));
			}
			if(map.get(funitses.get(i).getUnitsId()+"all").getRkzzl()!=null){
			rkviewMaterialZzlVos.add(map.get(funitses.get(i).getUnitsId()+"all"));
			}
			if(map.get(funitses.get(i).getUnitsId()+"all").getKczzl()!=null){
			kcviewMaterialZzlVos.add(map.get(funitses.get(i).getUnitsId()+"all"));
			}
		}

		return map;
	}
	
	private ViewMaterialZzlVo sum(ViewMaterialZzlVo view1,ViewMaterialZzl view2){
		view1.setCkMoney((view1.getCkMoney()!=null?view1.getCkMoney():new BigDecimal(0)).add(view2.getCkMoney()!=null?view2.getCkMoney():new BigDecimal(0)));
		view1.setKcMoney((view1.getKcMoney()!=null?view1.getKcMoney():new BigDecimal(0)).add(view2.getKcMoney()!=null?view2.getKcMoney():new BigDecimal(0)));
		view1.setRkMoney((view1.getRkMoney()!=null?view1.getRkMoney():new BigDecimal(0)).add(view2.getRkMoney()!=null?view2.getRkMoney():new BigDecimal(0)));
		return view1;
	}
}


class BykczzlComparator implements Comparator<Object>{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		if(((ViewMaterialZzl)o1).getKczzl()!=null&&((ViewMaterialZzl)o2).getKczzl()==null){
			return 1;
		}else if(((ViewMaterialZzl)o1).getKczzl()==null&&((ViewMaterialZzl)o2).getKczzl()!=null){
			return -1;
		}else if(((ViewMaterialZzl)o1).getKczzl()==null&&((ViewMaterialZzl)o2).getKczzl()==null){
			return 0;
		}
		Double a = ((ViewMaterialZzl)o1).getKczzl().doubleValue();
		Double b = ((ViewMaterialZzl)o2).getKczzl().doubleValue();
		Double diff = a-b;
		if(diff>0){
			return 1;
		}else if(diff<0){
			return -1;
		}else{
		return 0;
		}
	}
}

class ByrkzzlComparator implements Comparator<Object>{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		if(((ViewMaterialZzl)o1).getRkzzl()!=null&&((ViewMaterialZzl)o2).getRkzzl()==null){
			return 1;
		}else if(((ViewMaterialZzl)o1).getRkzzl()==null&&((ViewMaterialZzl)o2).getRkzzl()!=null){
			return -1;
		}else if(((ViewMaterialZzl)o1).getRkzzl()==null&&((ViewMaterialZzl)o2).getRkzzl()==null){
			return 0;
		}
		Double a = ((ViewMaterialZzl)o1).getRkzzl().doubleValue();
		Double b = ((ViewMaterialZzl)o2).getRkzzl().doubleValue();
		Double diff = a-b;
		if(diff>0){
			return 1;
		}else if(diff<0){
			return -1;
		}else{
		return 0;
		}
	}
}

class VoBykczzlComparator implements Comparator<Object>{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		if(o1!=null&&o2==null){
			return 1;
		}else if(o1==null&&o2!=null){
			return -1;
		}else if(o1==null&&o2==null){
			return 0;
		}
		Double a = Double.valueOf(((ViewMaterialZzlVo)o1).getKczzl().substring(0,((ViewMaterialZzlVo)o1).getKczzl().length()-1 ));
		Double b = Double.valueOf(((ViewMaterialZzlVo)o2).getKczzl().substring(0,((ViewMaterialZzlVo)o1).getKczzl().length()-1 ));
		Double diff = a-b;
		if(diff>0){
			return 1;
		}else if(diff<0){
			return -1;
		}else{
		return 0;
		}
	}
}

class VoByrkzzlComparator implements Comparator<Object>{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		if(o1!=null&&o2==null){
			return 1;
		}else if(o1==null&&o2!=null){
			return -1;
		}else if(o1==null&&o2==null){
			return 0;
		}
		Double a = Double.valueOf(((ViewMaterialZzlVo)o1).getRkzzl().substring(0,((ViewMaterialZzlVo)o1).getRkzzl().length()-1 ));
		Double b = Double.valueOf(((ViewMaterialZzlVo)o2).getRkzzl().substring(0,((ViewMaterialZzlVo)o1).getRkzzl().length()-1 ));
		Double diff = a-b;
		if(diff>0){
			return 1;
		}else if(diff<0){
			return -1;
		}else{
		return 0;
		}
	}
}

